package com.sskj.wechat.repository;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sskj.core.query.annotation.DynamicQuery;
import com.sskj.core.repository.GenericRepository;
import com.sskj.wechat.domain.WeChatUserQueryParam;
import com.sskj.wechat.domain.entity.WeChatUserEntity;

public interface WeChatUserRepository extends GenericRepository<WeChatUserEntity, String> {

	@Transactional
	@Modifying(clearAutomatically = true)
	@Query("update WeChatUserEntity x set x.subscribe=false where x.openId = ?1")
	void unSubscribe(String openId);

	@Query("from  WeChatUserEntity  x  where x.openId = ?1")
	public WeChatUserEntity findByOpenId(String openId);
	
	@Query("from  WeChatUserEntity  x  where x.openId = ?1 and x.isBinDing=true")
	public WeChatUserEntity findBindingUserByOpenId(String openId);

	@Query(value = "from WeChatUserEntity x where  1=1 "
	            + "<notEmpty name='nickname'> and x.nickname like '%${nickname}%'</notEmpty> "
	            + "<notEmpty name='telNum'> and x.telNum like '%${telNum}%'</notEmpty>  order by  x.nickname asc")
	@DynamicQuery
	Page<WeChatUserEntity> findUserPage(WeChatUserQueryParam weChatUserQueryParam, Pageable pageable);
}
